package com.qianfeng.iot.blackip.service.impl;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.dc3.common.exception.AddException;
import com.qianfeng.iot.blackip.dto.Dc3BlackIpDto;
import com.qianfeng.iot.blackip.events.CacheRefreshEvent;
import com.qianfeng.iot.blackip.mapper.Dc3BlackIpMapper;
import com.qianfeng.iot.blackip.pojo.Dc3BlackIp;
import com.qianfeng.iot.blackip.service.BlackIpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

/**
 * Created by Jackiechan on 2022/7/20 15:07
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
@Service
@Transactional
public class BlackIpServiceImpl implements BlackIpService {


    private ApplicationContext context;

    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    private Dc3BlackIpMapper dc3BlackIpMapper;


    @Autowired
    public void setDc3BlackIpMapper(Dc3BlackIpMapper dc3BlackIpMapper) {
        this.dc3BlackIpMapper = dc3BlackIpMapper;
    }

    @Override
    public void addBlackIp(Dc3BlackIp dc3BlackIp) {
        //判断一下数据
        Assert.hasText(dc3BlackIp.getIp(), () -> {
            throw new AddException("没有传递ip");
        });
        dc3BlackIpMapper.addBlackIp(dc3BlackIp);
        //刷新缓存,但是注意,这个操作应该是可选的操作
        refresh();
    }

    @Override
    public List<Dc3BlackIpDto> findAllIp2Dto() {
        return dc3BlackIpMapper.findAllIp2Dto();
    }

    @Override
    public Long deleteBlackIp(String id) {
        Long result = dc3BlackIpMapper.deleteBlackIp(id);
        refresh();
        return result;
    }

    @Override
    public void refresh() {
        context.publishEvent(new CacheRefreshEvent());
    }

}
